#ifndef TIMER_H
#define TIMER_H
#include <chrono>
#include <string>
namespace Timer
{
	inline std::chrono::steady_clock::time_point get_time_now()
	{
		timespec tp;
		clock_gettime(CLOCK_REALTIME, &tp);     // realtime mode is also efficient, and don't need adjust by secDiff.
		return std::chrono::steady_clock::time_point(
				std::chrono::steady_clock::duration(
						std::chrono::seconds(tp.tv_sec) + std::chrono::nanoseconds(tp.tv_nsec)
				)
		);
	}

	inline long getNano() 
	{
		return std::chrono::duration_cast<std::chrono::nanoseconds>(
				get_time_now().time_since_epoch()
		).count();
	}

	inline std::string getTime()
	{
		long nano = getNano();
		nano -= 1031230000;
		long sec = nano / 1000000000;
		long usec = nano % 1000000000 /1000;
		char ret[16]={0};
		struct tm* dt = localtime(&sec);
		strftime(ret,sizeof(ret), "%H:%M:%S",dt);
		return ret;
	}

	inline int nano2date(long nano)
	{
		nano -= 1031230000;
		long sec = nano / 1000000000;
		long usec = nano % 1000000000 /1000;
		char ret[16]={0};
		struct tm* dt = localtime(&sec);
		strftime(ret,sizeof(ret), "%Y%m%d",dt);
		return atoi(ret);
	}


	inline std::string nano2str(long nano)
	{
//		nano -= 1031230000;
		long sec = nano / 1000000000;
		long usec = nano % 1000000000 /1000;
		char ret[32]={0};
		struct tm* dt = localtime(&sec);
		strftime(ret,sizeof(ret), "%Y%m%d-%H:%M:%S.",dt);
		for(int i =0 ;i <6;i++)
		{
			int c = usec%10;
			usec /= 10;
			ret[23-i] = c+'0';
		}
		return ret;
	}
	inline long str2nano(const char* str)
	{
		struct tm t;
		strptime(str, "%Y%m%d-%H:%M:%S", &t);
		long ret = mktime(&t);
		return ret * long(1000000000);
	}
	inline int time2int(const char* T, int m)
	{
		return ( T[0]*100000 + T[1]*10000 + T[3]*1000 + T[4]*100 + T[6]*10 + T[7] - 5333328) * 1000 + m;
	}

	inline bool isClose(int t)
	{
		return t < 85900000 || (t >= 151500000  && t < 205900000) || t >= 230000000 ; // || (t >=113000000 && t <130000000);
	}

}
#endif
